<?php
/**
 * IndexController is the default controller for this application
 * 
 * Notice that we do not have to require 'Zend/Controller/Action.php', this
 * is because our application is using "autoloading" in the bootstrap.
 *
 * @see http://framework.zend.com/manual/en/zend.loader.html#zend.loader.load.autoload
 */
  
class IndexController extends Zend_Controller_Action 
{
    protected $model;
    protected $setting;
    protected $mote_gateway;
 
    protected function _getModel()
    {
        if (null === $this->model) {
            require_once APPLICATION_PATH . '/models/Room.php';
            $this->model = new Model_Room();
        }
        return $this->model;
    }    
    
    protected function _getSettingModel() 
    {
      if (null === $this->setting) {
          require_once APPLICATION_PATH . '/models/Setting.php';
          $this->setting = new Model_Setting();
      }
      return $this->setting;
    }
    

    protected function _getMoteGatewayModel()
    {
      if (null === $this->mote_gateway) {
        require_once APPLICATION_PATH . '/models/MoteGateway.php';
        $this->mote_gateway = new Model_MoteGateway();
      }
      return $this->mote_gateway;
    }

     
    /**
     * The "index" action is the default action for all controllers. This 
     * will be the landing page of your application.
     *
     * Assuming the default route and default router, this action is dispatched 
     * via the following urls:
     *   /
     *   /index/
     *   /index/index
     *
     * @return void
     */
    public function indexAction() 
    {

        $roomModel    = $this->_getModel();
        $setting      = $this->_getSettingModel();
        $this->view->mode   = ($setting->isHeat()    == true) ?  "heat" : "cool";
        $this->view->scale  = ($setting->isCelsius() == true) ?  "C"    : "F";
        $this->view->nav = "index";
        $this->view->rooms  = $roomModel->fetchEntries();
        
        // setmote interface
        $output = shell_exec(APPLICATION_PATH . '/../scripts/setcom.sh');
        //var_dump($output);
        //exit;
        // start listener
        //$this->_getMoteGatewayModel()->getReadings();
    }

}
